************************************
*** FIGURE 3: Effect of EMR System on Patient Lapse Probability

*** TABLE A4: Predictors of Patient Lapse Pre-EMR
*** TABLE A6: Effect of EMR System on Patient Lapse Probability, With and Without Controls
*** TABLE A7: Effect of EMR System on Patient Lapse Probability by Gender
*** TABLE A8: Effect of EMR System on Patient Lapse Probability by Age
************************************

use Data/Analysis/Data_IndividualYears, clear


local covars "YearsInit Male Age_* UnderweightInit AdvHIVInit"

foreach x of varlist hospital urban large {
	gen n`x'=1-`x'
}

cap erase Output/Tables/TableA04.xls
cap erase Output/Tables/TableA04.txt

local covars "YearsInit Male Age_* UnderweightInit AdvHIVInit"
forvalues i = 6 12 : 36 {
	reg Lapse`i' `covars' hospital large urban if Post==0 
		outreg2 using Output/Tables/TableA04.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) addtext(Year FE, N, Clinic FE, N, Controls, N) 
}


local covars "YearsInit Male Age_* UnderweightInit AdvHIVInit"
forvalues i = 6 12 : 36 {
	reghdfe Lapse`i' Post `covars', a(Datey VisitSite) cluster(VisitSite)
	estimates store e_Lapse`i'
	reghdfe Lapse`i' Post `covars' if Male==1, a(Datey VisitSite) cluster(VisitSite)
	estimates store e_Lapse`i'Male
	reghdfe Lapse`i' Post `covars' if Male==0, a(Datey VisitSite) cluster(VisitSite)
	estimates store e_Lapse`i'Female
}

local covars "YearsInit Male Age_* UnderweightInit AdvHIVInit"
forvalues i = 6 12 : 36 {
	reghdfe Lapse`i' Post `covars' if Age_09==1, a(Datey VisitSite) cluster(VisitSite)
	estimates store e_Lapse`i'Age_09
	reghdfe Lapse`i' Post `covars' if Age_1017==1, a(Datey VisitSite) cluster(VisitSite)
	estimates store e_Lapse`i'Age_1017
	reghdfe Lapse`i' Post `covars' if Age_1849==1, a(Datey VisitSite) cluster(VisitSite)
	estimates store e_Lapse`i'Age_1849
	reghdfe Lapse`i' Post `covars' if Age_50plus==1, a(Datey VisitSite) cluster(VisitSite)
	estimates store e_Lapse`i'Age_50plus

}

coefplot (e_Lapse6, label("Overall") msymbol(O) mcol(black) ciopts(recast(rcap) lcol(black))) ///
(e_Lapse6Male, label("Male") msymbol(T) mcol(green) ciopts(recast(rcap) lcol(green))) ///
(e_Lapse6Female, label("Female") msymbol(S) mcol(red) ciopts(recast(rcap) lcol(red))), bylabel(6 months) ///
|| (e_Lapse12, label("Overall") msymbol(O) mcol(black) ciopts(recast(rcap) lcol(black))) ///
(e_Lapse12Male, label("Male") msymbol(T) mcol(green%90) ciopts(recast(rcap) lcol(green%90))) ///
(e_Lapse12Female, label("Female") msymbol(S) mcol(red%90) ciopts(recast(rcap) lcol(red%90))), bylabel(12 months) ///
|| (e_Lapse18, label("Overall") msymbol(O) mcol(black) ciopts(recast(rcap) lcol(black))) ///
(e_Lapse18Male, label("Male") msymbol(T) mcol(green%90) ciopts(recast(rcap) lcol(green%90))) ///
(e_Lapse18Female, label("Female") msymbol(S) mcol(red%90) ciopts(recast(rcap) lcol(red%90))), bylabel(18 months) ///
|| (e_Lapse24, label("Overall") msymbol(O) mcol(black) ciopts(recast(rcap) lcol(black))) ///
(e_Lapse24Male, label("Male") msymbol(T) mcol(green%90) ciopts(recast(rcap) lcol(green%90))) ///
(e_Lapse24Female, label("Female") msymbol(S) mcol(red%90) ciopts(recast(rcap) lcol(red%90))), bylabel(24 months) ///
|| (e_Lapse30, label("Overall") msymbol(O) mcol(black) ciopts(recast(rcap) lcol(black))) ///
(e_Lapse30Male, label("Male") msymbol(T) mcol(green%90) ciopts(recast(rcap) lcol(green%90))) ///
(e_Lapse30Female, label("Female") msymbol(S) mcol(red%90) ciopts(recast(rcap) lcol(red%90))), bylabel(30 months) ///
|| (e_Lapse36, label("Overall") msymbol(O) mcol(black) ciopts(recast(rcap) lcol(black))) ///
(e_Lapse36Male, label("Male") msymbol(T) mcol(green%90) ciopts(recast(rcap) lcol(green%90))) ///
(e_Lapse36Female, label("Female") msymbol(S) mcol(red%90) ciopts(recast(rcap) lcol(red%90))), bylabel(36 months) ///
  ||, nolabels legend(rows(3)) keep(Post) xline(0) ytitle() byopts(noiylabel noiytick compact cols(1)) xlabel(-0.12(0.02)0.02) 
	graph save "Output/Figures/Figure03PanelA.gph", replace
	graph export "Output/Figures/Figure03PanelA.png", replace


coefplot (e_Lapse6Age_09, label("0-9") msymbol(O) mcol(blue) ciopts(recast(rcap) lcol(blue))) ///
(e_Lapse6Age_1017, label("10-17") msymbol(D) mcol(orange) ciopts(recast(rcap) lcol(orange))) ///
(e_Lapse6Age_1849, label("18-49") msymbol(T) mcol(green) ciopts(recast(rcap) lcol(green))) ///
(e_Lapse6Age_50plus, label("50+") msymbol(S) mcol(red) ciopts(recast(rcap) lcol(red))), bylabel(6 months) ///
|| (e_Lapse12Age_09, label("0-9") msymbol(O) mcol(blue) ciopts(recast(rcap) lcol(blue))) ///
(e_Lapse12Age_1017, label("10-17") msymbol(D) mcol(orange) ciopts(recast(rcap) lcol(orange))) ///
(e_Lapse12Age_1849, label("18-49") msymbol(T) mcol(green) ciopts(recast(rcap) lcol(green))) ///
(e_Lapse12Age_50plus, label("50+") msymbol(S) mcol(red) ciopts(recast(rcap) lcol(red))), bylabel(12 months) ///
|| (e_Lapse18Age_09, label("0-9") msymbol(O) mcol(blue) ciopts(recast(rcap) lcol(blue))) ///
(e_Lapse18Age_1017, label("10-17") msymbol(D) mcol(orange) ciopts(recast(rcap) lcol(orange))) ///
(e_Lapse18Age_1849, label("18-49") msymbol(T) mcol(green) ciopts(recast(rcap) lcol(green))) ///
(e_Lapse18Age_50plus, label("50+") msymbol(S) mcol(red) ciopts(recast(rcap) lcol(red))), bylabel(18 months) ///
|| (e_Lapse24Age_09, label("0-9") msymbol(O) mcol(blue) ciopts(recast(rcap) lcol(blue))) ///
(e_Lapse24Age_1017, label("10-17") msymbol(D) mcol(orange) ciopts(recast(rcap) lcol(orange))) ///
(e_Lapse24Age_1849, label("18-49") msymbol(T) mcol(green) ciopts(recast(rcap) lcol(green))) ///
(e_Lapse24Age_50plus, label("50+") msymbol(S) mcol(red) ciopts(recast(rcap) lcol(red))), bylabel(24 months) ///
|| (e_Lapse30Age_09, label("0-9") msymbol(O) mcol(blue) ciopts(recast(rcap) lcol(blue))) ///
(e_Lapse30Age_1017, label("10-17") msymbol(D) mcol(orange) ciopts(recast(rcap) lcol(orange))) ///
(e_Lapse30Age_1849, label("18-49") msymbol(T) mcol(green) ciopts(recast(rcap) lcol(green))) ///
(e_Lapse30Age_50plus, label("50+") msymbol(S) mcol(red) ciopts(recast(rcap) lcol(red))), bylabel(30 months) ///
|| (e_Lapse36Age_09, label("0-9") msymbol(O) mcol(blue) ciopts(recast(rcap) lcol(blue))) ///
(e_Lapse36Age_1017, label("10-17") msymbol(D) mcol(orange) ciopts(recast(rcap) lcol(orange))) ///
(e_Lapse36Age_1849, label("18-49") msymbol(T) mcol(green) ciopts(recast(rcap) lcol(green))) ///
(e_Lapse36Age_50plus, label("50+") msymbol(S) mcol(red) ciopts(recast(rcap) lcol(red))), bylabel(36 months) ///
  ||, nolabels legend(rows(4)) keep(Post) xline(0) ytitle() byopts(noiylabel noiytick compact cols(1)) xlabel(-0.12(0.02)0.02) 
	graph save "Output/Figures/Figure03PanelB.gph", replace
	graph export "Output/Figures/Figure03PanelB.png", replace


***APPENDIX TABLES***


cap erase Output/Tables/TableA06.xls
cap erase Output/Tables/TableA06.txt

*No covariates
local covars ""
forvalues i = 6 12 : 36 {
	reghdfe Lapse`i' Post `covars', a(Datey VisitSite) cluster(VisitSite)
		outreg2 using Output/Tables/TableA06.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Post) addtext(Year FE, Y, Clinic FE, Y, Controls, N) 
}
*Covariates
local covars "YearsInit Male Age_* UnderweightInit AdvHIVInit"
forvalues i = 6 12 : 36 {
	reghdfe Lapse`i' Post `covars', a(Datey VisitSite) cluster(VisitSite)
		outreg2 using Output/Tables/TableA06.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Post) addtext(Year FE, Y, Clinic FE, Y, Controls, Y) 
}

cap erase Output/Tables/TableA07.xls
cap erase Output/Tables/TableA07.txt

*Males 
local covars "YearsInit Male Age_* UnderweightInit AdvHIVInit"
forvalues i = 6 12 : 36 {
	reghdfe Lapse`i' Post `covars' if Male==1, a(Datey VisitSite) cluster(VisitSite)
		outreg2 using Output/Tables/TableA07.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Post) addtext(Year FE, Y, Clinic FE, Y, Controls, Y) 
}
*Females
local covars "YearsInit Male Age_* UnderweightInit AdvHIVInit"
forvalues i = 6 12 : 36 {
		reghdfe Lapse`i' Post `covars' if Male==0, a(Datey VisitSite) cluster(VisitSite)
		outreg2 using Output/Tables/TableA07.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Post) addtext(Year FE, Y, Clinic FE, Y, Controls, Y) 
}

cap erase Output/Tables/TableA08.xls
cap erase Output/Tables/TableA08.txt

*Robustness - Ages
local covars "YearsInit Male Age_* UnderweightInit AdvHIVInit"
forvalues i = 6 12 : 36 {
	reghdfe Lapse`i' Post `covars' if Age_09==1, a(Datey VisitSite) cluster(VisitSite)
		outreg2 using Output/Tables/TableA08.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Post) addtext(Year FE, Y, Clinic FE, Y, Controls, Y) 
	reghdfe Lapse`i' Post `covars' if Age_1017==1, a(Datey VisitSite) cluster(VisitSite)
		outreg2 using Output/Tables/TableA08.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Post) addtext(Year FE, Y, Clinic FE, Y, Controls, Y) 
	reghdfe Lapse`i' Post `covars' if Age_1849==1, a(Datey VisitSite) cluster(VisitSite)
		outreg2 using Output/Tables/TableA08.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Post) addtext(Year FE, Y, Clinic FE, Y, Controls, Y) 
	reghdfe Lapse`i' Post `covars' if Age_50plus==1, a(Datey VisitSite) cluster(VisitSite)
		outreg2 using Output/Tables/TableA08.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Post) addtext(Year FE, Y, Clinic FE, Y, Controls, Y) 
}
